package com.xb.controller;

import com.xb.entity.Role;
import com.xb.service.IMenuService;
import com.xb.service.IRoleService;
import com.xb.util.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Controller
 * /api/role
 */
@RestController
@RequestMapping("/api/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IMenuService menuService;

    /**
     * 获取全部角色
     *
     * @return 结果集
     */
    @RequestMapping("/getAllRole")
    @ResponseBody
    public Result getAllRole() {
        Result result = new Result();
        List<Role> roles = roleService.selectAllRole();
        if (roles != null) {
            result.setStatus(Result.RESPONSE_SUCCESS);
            result.setData(roles);
        } else {
            result.setStatus(Result.RESPONSE_FAIL);
            result.setMessage("获取角色失败！请联系管理员 <a href=\"mailto:xb.yang1@outlook.com\">点击这里发送邮件给xb.yang1@outlook.com</a>");
        }
        return result;

    }

    @RequestMapping("/page")
    @RequiresPermissions("system:role:page")
    public Object findPage(Role obj, @RequestParam(value = "pageNum", defaultValue = "1") Integer page,
                           @RequestParam(value = "pageSize", defaultValue = "10") Integer limit) {
        return roleService.selectRoleList(obj, page, limit);
    }

    /**
     * 添加
     */
    @PostMapping
    @RequiresPermissions("system:role:add")
    public Object add(@RequestBody Role obj) {
        return roleService.add(obj);
    }

    /**
     * 修改
     */
    @PutMapping
    @RequiresPermissions("system:role:update")
    public Object update(@RequestBody Role obj) {
        return roleService.update(obj);
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    @RequiresPermissions("system:role:del")
    public Object del(@PathVariable Integer id) {
        return roleService.del(id);
    }
    @GetMapping("/getRole")
    public Result getRole(){
        return roleService.getRole();
    }

}
